package control;

import dao.GetMembers;
import entity.Check_inRecords;
import entity.Meeting;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import java.net.URL;
import java.util.ResourceBundle;

public class MeetingManageController implements Initializable {
    private Meeting meeting;
    private int type;
    @FXML
    private Label meetingId,tip1,tip2;
    @FXML
    private TextField meetingNameField,roomField,addField,deleteField,codeField;
    @FXML
    private TextArea introduceArea;
    @FXML
    private Button selectRoomButton,bookButton,saveButton,cancelButton,addButton,deleteButton;
    @FXML
    private DatePicker beginDatePicker,endDatePicker;
    @FXML
    private ComboBox<String> hh1,mm1,hh2,mm2;
    private final ObservableList<String> hours= FXCollections.observableArrayList();
    private final ObservableList<String> mins=FXCollections.observableArrayList();
    @FXML
    private TableView<Check_inRecords> memberTable;
    private final ObservableList<Check_inRecords> memberTableSource=FXCollections.observableArrayList();
    @Override
    public void initialize(URL location, ResourceBundle resources){
        //模拟时间
        for(int i=0;i<=9;i++){
            hours.add("0"+i);
            mins.add("0"+i);
        }
        for(int i=10;i<=23;i++){
            hours.add(i+"");
            mins.add(i+"");
        }
        for(int i=24;i<=59;i++){
            mins.add(i+"");
        }
        hh1.setItems(hours);
        hh2.setItems(hours);
        mm1.setItems(mins);
        mm2.setItems(mins);
        if(type==1||type==2){
            //我的会议,管理会议
            memberTableInit(memberTable,memberTableSource);
            meetingId.setText(meeting.getId());
            meetingNameField.setText(meeting.getName());
            roomField.setText(meeting.getRoom_id());
            introduceArea.setText(meeting.getIntroduce());
            beginDatePicker.setValue(meeting.getBegin().toLocalDate());
            hh1.setValue(meeting.getBegin().getHour()+"");
            mm1.setValue(meeting.getBegin().getMinute()+"");
            endDatePicker.setValue(meeting.getEnd().toLocalDate());
            hh2.setValue(meeting.getEnd().getHour()+"");
            mm2.setValue(meeting.getEnd().getMinute()+"");
            tip1.setVisible(false);
            tip2.setVisible(false);
            if(type==1){
                meetingNameField.setEditable(false);
                roomField.setEditable(false);
                selectRoomButton.setVisible(false);
                introduceArea.setEditable(false);
                beginDatePicker.setEditable(false);
                beginDatePicker.setDisable(true);
                hh1.setEditable(false);
                hh1.setDisable(true);
                mm1.setEditable(false);
                mm1.setDisable(true);
                endDatePicker.setEditable(false);
                endDatePicker.setDisable(true);
                hh2.setEditable(false);
                hh2.setDisable(true);
                mm2.setEditable(false);
                mm2.setDisable(true);
                bookButton.setVisible(false);
                saveButton.setVisible(false);
                cancelButton.setVisible(false);
                addButton.setVisible(false);
                deleteButton.setVisible(false);
                addField.setVisible(false);
                deleteField.setVisible(false);
            }else codeField.setText(meeting.getCode());
        }
    }
    public MeetingManageController(int type,Meeting meeting){
        this.type=type;
        this.meeting=meeting;
        memberTableSource.addAll(GetMembers.getMeetingMembers(meeting.getId()));
    }
    @FXML
    private Button closeButton,minimizeButton;
    @FXML
    private void close(){
        Stage stage = (Stage) closeButton.getScene().getWindow();
        stage.close();
    }
    @FXML
    private void minimize() {
        Stage stage = (Stage) minimizeButton.getScene().getWindow();
        stage.setIconified(true);
    }
    private void memberTableInit(TableView<Check_inRecords> tableView, ObservableList<Check_inRecords> source){
        ObservableList<TableColumn<Check_inRecords, ?>> TableColumnscolumns=tableView.getColumns();
        TableColumnscolumns.get(0).setCellValueFactory(new PropertyValueFactory<>("id"));
        TableColumnscolumns.get(1).setCellValueFactory(new PropertyValueFactory<>("name"));
        TableColumnscolumns.get(2).setCellValueFactory(new PropertyValueFactory<>("Check_inTime"));
        TableColumnscolumns.get(3).setCellValueFactory(new PropertyValueFactory<>("duration"));
        tableView.setItems(source);

    }
}
